﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WISD.Common;
using WISD.CarFriend.Model;
using WISD.CarFriend.BLL.OA;
using WISD.CarFriend.BLL.Web;
using System.Data;

namespace WISD.CarFriend.Web.OA
{
    public partial class Regist : System.Web.UI.Page
    {
        #region 事件
        /// <summary>
        /// Page_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetToolTip();
                this.txtMemberID.Text = MemberModule.GetMemberId();
            }
        }


        /// <summary>
        /// 注册保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                WebModule bll = new WebModule();
                if (bll.ValidMemberID(txtMemberID.Text.Trim()))
                {
                    string script = "";
                    script += "<script type=\"text/javascript\">\n";
                    script += "parent.jsprint(\"" + "该用户编号已经注册过！" + "\",\"" + "" + "\",\"" + "Error" + "\")\n";
                    script += "</script>\n";
                    this.Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", script);
                    return;
                }
                if (bll.ValidWebUserName(txtWebUserName.Text.Trim()))
                {
                    string script = "";
                    script += "<script type=\"text/javascript\">\n";
                    script += "parent.jsprint(\"" + "该用户名已经注册过！" + "\",\"" + "" + "\",\"" + "Error" + "\")\n";
                    script += "</script>\n";
                    this.Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", script);
                    return;
                }
                if (txtWebPwd.Text.Trim() != txtWebPwdCheck.Text.Trim())
                {
                    string script = "";
                    script += "<script type=\"text/javascript\">\n";
                    script += "parent.jsprint(\"" + "密码与确认密码不一致！" + "\",\"" + "" + "\",\"" + "Error" + "\")\n";
                    script += "</script>\n";
                    this.Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", script);
                    return;
                }

                T_CF_OA_Member member = new T_CF_OA_Member();
                member.MemberID = txtMemberID.Text.Trim();
                member.WebUserName = txtWebUserName.Text.Trim();
                member.WebPwd = txtWebPwd.Text.Trim();
                member.Name = txtName.Text.Trim();
                member.Sex = int.Parse(ddl_sex.SelectedValue);
                member.Birthday = ConvertToDate(txtBirthday.Text);//to时间格式
                member.MobileTel = txtMobileTel.Text.Trim();
                member.QQ = txtQQ.Text.Trim();
                member.Email = txtEmail.Text.Trim();
                member.IdentityCard = txtIdentityCard.Text.Trim();
                member.Address = Request.Form["ddlProvince"] + "/" + Request.Form["ddlCity"] + "/" + Request.Form["ddlCountry"];
                member.ReturnVisitDate = ConvertToDate(txtReturnVisitDate.Text);//to时间格式
                member.VehicleNo = txtVehicleNo.Text.Trim();
                member.VehicleStyle = txtVehicleStyle.Text.Trim();
                member.EngineNo = txtEngineNo.Text.Trim();
                member.VehicleFrameNo = txtVehicleFrameNo.Text.Trim();
                member.DriveCardNo = txtDriveCardNo.Text.Trim();
                member.DriveCardAnnualReviewDate = ConvertToDate(txtDriveCardAnnualReviewDate.Text);//to时间格式
                member.DriveCardGetDate = ConvertToDate(txtDriveCardGetDate.Text);//to时间格式
                member.RideCardNo = txtRideCardNo.Text.Trim();
                member.RideCardAnnualReviewDate = ConvertToDate(txtRideCardAnnualReviewDate.Text);//to时间格式
                member.PurchaseDate = ConvertToDate(txtPurchaseDate.Text);//to时间格式
                member.NextMaintenanceDate = ConvertToDate(txtNextMaintenanceDate.Text);//to时间格式
                member.VehicleAnnualInspectionDate = ConvertToDate(txtVehicleAnnualInspectionDate.Text);//to时间格式
                member.InsuranceNo = txtInsuranceNo.Text.Trim();
                member.InsuranceExpirationDate = ConvertToDate(txtInsuranceExpirationDate.Text);//to时间格式
                member.OilConsumption = txtOilConsumption.Text.Trim();
                member.DriveKilometers = txtDriveKilometers.Text.Trim();

                bll.AddRegisterMember(member);
                Session["UserID"] = member.MemberID;
                Session["UserName"] = member.Name;

                if (ddlOpenCard.SelectedValue == "1")
                {
                    MessageBox.AlertAndGo(this.Page, member.Name + "用户注册成功！", "RegistCard.aspx?MobileTel=" + member.MobileTel + "&VehicleNo=" + member.VehicleNo + "&VehicleStyle=" + member.VehicleStyle);
                }
                else
                {
                    MessageBox.AlertAndGo(this.Page, member.Name + "用户注册成功！", "../../Index.aspx");
                }
            }
            catch
            { }
        }
        #endregion


        #region 方法
        /// <summary>
        /// 字符转换为时间格式
        /// </summary>
        /// <param name="dateValue"></param>
        /// <returns></returns>
        private DateTime ConvertToDate(string dateValue)
        {
            DateTime dt1;
            if (dateValue.Trim() == "")
            {
                dt1 = DateTime.Now;
            }
            else
            {
                DateTime.TryParse(dateValue.Trim(), out dt1);
            }
            return dt1;
        }


        /// <summary>
        /// 绑定页面控件的ToolTip属性
        /// </summary>
        private void SetToolTip()
        {
            DataSet ds = ToolTipManageModule.GetList(" TableName ='T_CF_OA_Member' ");
            if (ds != null && ds.Tables.Count != 0)
            {
                ControlCollection ControlList = this.form1.Controls;
                foreach (System.Web.UI.Control Controls in ControlList)
                {
                    if (Controls.GetType().Equals(typeof(System.Web.UI.WebControls.TextBox)))
                    {
                        TextBox tb = (TextBox)Controls;

                        string tip = tb.Attributes["tip"];
                        foreach (DataRow r in ds.Tables[0].Rows)
                        {
                            if (tip == r["Markup"].ToString())
                            {
                                tb.Attributes.Add("HintTitle", r["Title"].ToString());
                                tb.Attributes.Add("HintInfo", r["Content"].ToString());
                                break;
                            }
                        }
                    }
                }
            }
        }
        #endregion
    }
}